package com.iwomy.secureplat.platform.utils;

import com.iwomy.secureplat.admin.api.feign.RemoteDeptService;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class RemoteDeptUtils {

	@Autowired
	private RemoteDeptService remoteDeptService;

	public <T, R> List<R> getPropertyList(List<T> entityList, Function<T, R> propertyGetter) {
		if (entityList == null || entityList.isEmpty()) {
			return Collections.emptyList();
		} else {
			return entityList.stream().map(propertyGetter).collect(Collectors.toList());
		}
	}

	public Map<Long, String> getDeptNames(List<Long> ids) {
		Map<Long, String> deptMap = null;
		if (ids != null && ids.stream().anyMatch(id -> id != null)) {
			List<Map<String, Object>> depts = (List<Map<String, Object>>) remoteDeptService.getByIds(ids).getData();
			deptMap = depts.stream()
					.collect(Collectors.toMap(
							dept -> Long.valueOf(dept.get("deptId").toString()),
							dept -> (String) dept.get("deptName")));
		}
		return deptMap;
	}

	public String getDeptName(Long deptid) {
		Map<String, Object> data = (Map<String, Object>) remoteDeptService.getById(deptid).getData();
		if (data != null) {
			String deptName = (String) data.getOrDefault("name", null);
			return deptName;
		}
		return null;
	}

	public Map<String, Object> getDept(Long deptid) {
		Map<String, Object> data = (Map<String, Object>) remoteDeptService.getById(deptid).getData();
		return data;
	}

	public R getAllDeptList(String deptName) {
		return remoteDeptService.getAllDeptList(deptName);
	}

	public R getAllDept() {
		return remoteDeptService.getAllDept();
	}

	public R getDescendantList(Long deptid) {
		return remoteDeptService.getDescendantList(deptid);
	}

	public R getChildrenList(Long deptid[]) {
		return remoteDeptService.getChildrenList(deptid);
	}

	public R getAuthorizeById(Long deptId) {
		return remoteDeptService.getAuthorizeById(deptId);
	}

	public R<List<Long>> listByDeptId(Long id) {
		return remoteDeptService.listByDeptId(id);
	}

	/**
	 * 通过部门名查询部门id
	 */
	public R getDeptId(String deptName) {
		return remoteDeptService.getDeptId(deptName);
	}

	public Map<String, Object> getExpenses(Long deptId) {
		Map<String, Object> data = (Map<String, Object>) remoteDeptService.getExpenses(deptId).getData();
		return data;
	}

	public String getExpensesDeptName(Long deptId) {
		Map<String, Object> data = getExpenses(deptId);
		if (data != null) {
			String deptName = (String) data.getOrDefault("name", null);
			return deptName;
		}
		return null;
	}

	//查询父级id
	public Long getParentId(Long deptId){
		Map<String, Object> data = this.getExpenses(deptId);
		String parentIdString = (String) data.getOrDefault("parentId", null);
		Long parentId = Long.parseLong(parentIdString);
		return parentId;
	}
}
